/*
 * SelectFrame.java
 *
 * Created on February 18, 2007, 11:17 PM
 */

package edu.byu.isys413.group1E.gui;

import edu.byu.isys413.group1E.data.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author  tkulbeth
 */
public class SelectFrame extends javax.swing.JFrame {
    
    CustomerFrame callingFrame = null;
    ArrayList<CustomerBO> custList = null;
    
    /**
     * Creates new form SelectFrame
     */
    public SelectFrame() {
        initComponents();
    }
    
    /**
     * Creates new form SelectFrame using a list to populate
     */
    public SelectFrame(ArrayList<CustomerBO> custList, CustomerFrame frame) {
        initComponents();
        this.custList = custList;
        callingFrame = frame;
        ArrayList<String> updatedList = new ArrayList<String>();
        //configure input for table
        for(int x=0;x<custList.size();x++){
            String custItem = custList.get(x).getCustFirstName() + " " + custList.get(x).getCustMI() + " " +
                    custList.get(x).getCustLastName() + ", " + custList.get(x).getCustAddr() + ", " + custList.get(x).getCustCity() +
                    ", " + custList.get(x).getCustState() + " " + custList.get(x).getCustZip() + ", " + custList.get(x).getCustPhone() +
                    ", " + custList.get(x).getCustEmail();
            
            updatedList.add(custItem);
        }
        this.list_Customers.setListData(updatedList.toArray());
        this.list_Customers.updateUI();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        button_CancelCustSelect = new javax.swing.JToggleButton();
        button_custSelect = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_Customers = new javax.swing.JList();
        label_custListMenu = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select a Customer");
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel1.setText("Select the Customer:");

        button_CancelCustSelect.setText("Cancel");
        button_CancelCustSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_CancelCustSelectActionPerformed(evt);
            }
        });

        button_custSelect.setText("Select");
        button_custSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_custSelectActionPerformed(evt);
            }
        });

        list_Customers.setFont(new java.awt.Font("Dialog", 1, 10));
        jScrollPane1.setViewportView(list_Customers);

        label_custListMenu.setFont(new java.awt.Font("Dialog", 0, 12));
        label_custListMenu.setText("Name, Address, City, ST, Zip, Phone, Email");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(button_custSelect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_CancelCustSelect))
                    .addComponent(label_custListMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_custListMenu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_CancelCustSelect)
                    .addComponent(button_custSelect))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void button_CancelCustSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_CancelCustSelectActionPerformed
        int opt = JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel?", "Cancel warning", JOptionPane.YES_NO_OPTION);
        if(opt == JOptionPane.YES_OPTION){
            this.setVisible(false);
            this.dispose();
        }else{
            this.button_custSelect.setSelected(false);
            callingFrame.setButton_UseCust(true);
        }
    }//GEN-LAST:event_button_CancelCustSelectActionPerformed
    
    private void button_custSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_custSelectActionPerformed
        //check if an item has been selected
        if(list_Customers.getSelectedIndex() != -1){
            if(list_Customers.getSelectedIndices().length > 1){
                JOptionPane.showMessageDialog(null, "Please only select one customer.");
                this.button_custSelect.setSelected(false);
                return;
            }else{      //when only one customer is selected
                //send the selected customer object back to the GUI for input
                callingFrame.setFrameEnabled(true);
                callingFrame.setCust(custList.get(list_Customers.getSelectedIndex()));
                callingFrame.setButton_SaveText("Save");
                callingFrame.setLabel_EnterDataText("View/Update Customer Data:");
                if(custList.get(list_Customers.getSelectedIndex()).getCustMembership() == null){
                    callingFrame.setButton_EditMembState(false);
                    callingFrame.setChkBox_NoState(true);
                }else{
                    callingFrame.setButton_EditMembState(true);
                    callingFrame.setChkBox_YesState(true);
                    callingFrame.setChkBox_NoState(false);
                    callingFrame.setChkBox_NoEnabled(false);
                    callingFrame.setLabel_memberText("Is member?");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null, "Please select a customer.");
        }
        this.button_custSelect.setSelected(false);
        this.setVisible(false);
        this.dispose();
        return;
    }//GEN-LAST:event_button_custSelectActionPerformed
    
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new SelectFrame().setVisible(true);
//            }
//        });
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton button_CancelCustSelect;
    private javax.swing.JToggleButton button_custSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_custListMenu;
    private javax.swing.JList list_Customers;
    // End of variables declaration//GEN-END:variables
    
}
